package net.gyapp.markim.module.message;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.easyder.wrapper.base.view.WrapperActivity;
import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;
import com.gyf.barlibrary.ImmersionBar;

import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.winds.widget.component.SolveViewPager;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/8/2
 * Version: 1.0
 * Desc:
 */


public class ImagePreviewActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.mViewPager)
    SolveViewPager mViewPager;

    public static Intent getIntent(Context context, List<String> list) {
        return new Intent(context, ImagePreviewActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .putStringArrayListExtra("list", new ArrayList<>(list));
    }

    public static Intent getIntent(Context context, String path) {
        List<String> list = new ArrayList<>();
        list.add(path);
        return getIntent(context, list);
    }

    @Override
    protected void setImmersionBar() {
        ImmersionBar.with(this).fullScreen(true).init();
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_image_preview;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        setInterceptable(false);
        getContainerView().setBackgroundColor(ContextCompat.getColor(mActivity, R.color.textSuper));
        mViewPager.setAdapter(new ImageAdapter(mActivity, intent.getStringArrayListExtra("list")));
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            return super.dispatchTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected int getTitleState() {
        return TITLE_NONE;
    }

    private static class ImageAdapter extends PagerAdapter {

        private List<String> datas;
        private LayoutInflater inflater;
        private Context context;


        public ImageAdapter(Context context, List<String> datas) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas == null ? 0 : datas.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = inflater.inflate(R.layout.item_image_details, container, false);
            if (view != null) {
                final ImageView imageView = view.findViewById(R.id.image);

                //loading
                final ProgressBar loading = new ProgressBar(context);
                FrameLayout.LayoutParams loadingLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                loadingLayoutParams.gravity = Gravity.CENTER;
                loading.setLayoutParams(loadingLayoutParams);
                ((FrameLayout) view).addView(loading);

                final String imgurl = datas.get(position);


                Glide.with(context)
                        .load(imgurl)
                        .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                        .into(new DrawableImageViewTarget(imageView) {
                            @Override
                            public void onLoadStarted(@Nullable Drawable placeholder) {
                                super.onLoadStarted(placeholder);
                                loading.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                                super.onLoadFailed(errorDrawable);
                                loading.setVisibility(View.GONE);
                            }

                            @Override
                            public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                super.onResourceReady(resource, transition);
                                loading.setVisibility(View.GONE);
                            }
                        });


                container.addView(view, 0);
            }
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
